/*
* $Id: Control.cpp 2 2009-12-23 13:58:02Z volok-aleksej@yandex.ru $
*
* Copyright 2008 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "InterfaceDC.h"
#include "Settings/DinIntDefined.h"
#include "../../UserMessage.h"
#include "Control.h"

CControl::CControl(const std::tstring& strId, const std::tstring& strState, const std::vector<Parameter>& aParameters)
: m_strId(strId), m_strState(strState), m_aParameters(aParameters.begin(), aParameters.end()){
	m_pFont = CCommonFunction::GetFont(_T("MS Sans Serif"), 14, FALSE, FALSE);
}
CControl::~CControl(){
	delete m_pFont;
}
std::tstring CControl::GetId(){
	return m_strId;
}
void CControl::SetState(const std::tstring& strState){
	m_strState = strState;
}
std::tstring CControl::GetState(){
	return m_strState;
}
bool CControl::Validate(){
	std::vector<Parameter>::iterator it = std::find(m_aParameters.begin(), m_aParameters.end(), REGEX);
	if(it != m_aParameters.end()){
		std::tstring strReg = it->GetValue();
		std::tstring value = GetState();
		if(!CCommonFunction::ValidRegex(value, strReg)){
			std::tstring strRuleDiscribe = GetRuleDescribe();
			if(!strRuleDiscribe.empty()){
				tooltip.SetTitle(1, strRuleDiscribe.c_str());
				tooltip.Activate(true);
			}
			return false;
		}
	}
	tooltip.Activate(false);
	return true;
}
std::tstring CControl::GetRuleDescribe(){
	std::vector<Parameter>::iterator it = std::find(m_aParameters.begin(), m_aParameters.end(), RULE_DESCRIBE);
	if(it != m_aParameters.end()){
		return it->GetValue();}
	return _T("");
}
bool CControl::IsTabStop(){
	return false;
}
void CControl::TabStop(){}
void CControl::SendEvent(){
	CWnd* pParent = dynamic_cast<CWnd*>(this)->GetParent();
	pParent->SendMessage(UM_SETSTATE, (WPARAM)m_strId.c_str(), (LPARAM)m_strState.c_str());
	for(std::vector<Parameter>::iterator it = m_aParameters.begin(); it != m_aParameters.end(); it++){
		if(it->GetName() == DEPENDENTS)
			pParent->SendMessage(UM_DEPENDENT, 0, (LPARAM)it->GetValue().c_str());
	}
}
void CControl::Free(){
	delete this;
}